// Terrain.java
//
// The basic terrain class. It's a very customizable framework.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.nio.*;

public class Terrain
{
    private int num; // The ID number of this terrain
    private String name;
    private String desc;
    private Boolean passable;
    private BufferedImage img;
    private BufferedReader reader;
    
    public Terrain(int terraNum)
    {
        try	
        {
            // Set ID number and default values
            num = terraNum;
            name = "Nothing";
            desc = "Nothing is here! ERROR: This should not show up through normal gameplay!";
            passable = true;
            img = ImageIO.read(new File("Terrains\\Images\\Blank.png"));
            
            // read each line from the terrain's config file into a temporary string
            // and then set the terrain's variables based on that string
            String file = "Terrains\\" + num + ".txt";
            reader = new BufferedReader(new FileReader(file));
            String temp;
            String cat, value;
            while(true)
            {
                temp = reader.readLine();
                if (temp.contains("END")) {break;}
                cat = temp.substring(0, temp.indexOf("="));
                value = temp.substring(temp.indexOf("=") + 1);
                
                switch (cat)
                {
                    case "Name":
                    {
                        name = value;
                        break;   
                    }
                    case "Desc":
                    {
                        desc = value;
                        break;   
                    }
                    case "Image":
                    {
                        img = ImageIO.read(new File(value));
                        break;   
                    }
                    case "Passable":
                    {
                        passable = Boolean.valueOf(value);   
                    }
                    default:
                    {
                        break;   
                    }   
                }   
            }	
            reader.close();
        }
        catch(IOException e)
        {
            System.out.println("Uh oh! Something broke with the file reading!");
            System.out.println(e);	
        }
    }
    
    public void display(int x, int y, Graphics g)
    {
        g.drawImage(img, x, y, null);
    }
    
    public void tick()
    {	
    }

    // Is the terrain passable?
    public boolean isPassable()
    {
        return passable;
    }
    
    // print the description
    public void examine()
    {
        System.out.println(desc.substring(0,1).toUpperCase() + desc.substring(1));
    }
    // GET AND SET METHODS
    public int getNum()
    {
        return num;
    }
        
    public String getName()
    {
        return name;
    }
        
    public void setName(String newName)
    {
        name = newName;	
    }

    public String getDesc()
    {
        return desc;
    }

    public void setDesc(String newDesc)
    {
        desc = newDesc;
    }
    
    public void setImg(BufferedImage newImg)
    {
        img = newImg;
    }
    // END GETS/SETS

    public String toString()
    {
        return ("///////////////" +
                "\n//Class: Terrain" +
                "\n//ID Number: " + num +
                "\n//Name: " + name +
                "\n//Description: " + desc +
                "\n//Passable: " + passable
                + "\n///////////////");
    }
}